package cn.jingdalei.basic.service.impl;

import cn.jingdalei.basic.mapper.BaseMapper;
import cn.jingdalei.basic.query.BaseQuery;
import cn.jingdalei.basic.service.IBaseService;
import cn.jingdalei.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    
    @Autowired
    private BaseMapper<T> mapper;
    
    @Override
    @Transactional
    public void add(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        mapper.remove(id);
    }

    @Override
    public T queryById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public List<T> queryAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList<T> queryData(BaseQuery query) {
        // 查询总条数
        Integer count = mapper.queryCount(query);
        // 查询每页展示的内容
        List<T> queryData = mapper.queryData(query);

        return new PageList<T>(count,queryData);
    }

    @Override
    @Transactional
    public void patchDel(List<Long> ids) {
        mapper.patchRemove(ids);
    }
}
